package sessionBean;

import interfaceBeanRemote.sessionCommentaireRemote;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import entityBean.Administrateur;
import entityBean.Commentaire;
import entityBean.Investisseur;

/**
 * Session Bean implementation class sessionAdministrateur
 */
@Stateless
public class sessionCommentaire implements sessionCommentaireRemote {
	@PersistenceContext(unitName = "EJB3_Entity")
	EntityManager em;

	@PostConstruct
	public void init() {
		System.out.println("calling init method");
	}

	@PreDestroy
	public void cleanup() {
		System.out.println("calling cleanup method");
	}

	/**
	 * Default constructor.
	 */
	public sessionCommentaire() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public Commentaire addCommentaire(Commentaire com) {
		em.persist(com);
		return com;
	}

	@Override
	public Commentaire findCommentaire(int id) {
		// TODO Auto-generated method stub
		return em.find(Commentaire.class, id);
	}

	@Override
	public void removeCommentaireById(int id) {
		em.remove(em.find(Commentaire.class, id));

	}

	@Override
	public void updateCommentaire(Commentaire com) {
		em.merge(com);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Commentaire> getCommentaireList() {
		// TODO Auto-generated method stub
		Query query = em.createQuery("SELECT c FROM Commentaire AS c left join fetch c.investisseur"
				+ " ORDER BY c.startup.idStartUp ASC");
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Commentaire> getCommentaireListByIdStartUp(int idStartUp) {
		// TODO Auto-generated method stub
		Query query = em
				.createQuery("SELECT c FROM Commentaire AS c where c.startup.idStartUp = " + idStartUp 
						+ " ORDER BY c.idCommentaire ASC");
		return query.getResultList();
	}

	@Override
	public void refresh(Commentaire com) {
		// TODO Auto-generated method stub
		em.refresh(com);
	}

	@Override
	public void flush() {
		// TODO Auto-generated method stub
		em.flush();
	}

}